package org.example.edusmart.controller.UserInfo;

import jakarta.servlet.http.HttpServletRequest;
import org.example.edusmart.pojo.UserPojo;
import org.example.edusmart.service.UserService;
import org.example.edusmart.utils.GetTokenInfo;
import org.example.edusmart.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/userInfo")
public class GetUserInfoController {

    @Autowired
    private GetTokenInfo getTokenInfo;

    @Autowired
    private UserService userService;

    /**
     * 查看用户个人信息
     * @param request 请求头
     * @return 用户个人信息
     */
    @PostMapping("/getUserInfo/")
    @PreAuthorize("hasAuthority('public_permission')") //指明有公共权限的才能访问
    public Result getUserInfo(HttpServletRequest request){
        //获取用户ID
        String userId = getTokenInfo.getUserIdByJwt(request);
        //根据id获得该用户
        UserPojo user = userService.getUserByUserId(userId);
        List<HashMap<String,Object>> userInfo = new ArrayList<>();
        HashMap<String,Object> info = new HashMap<>(){{
            put("avatar",user.getAvatar());
            put("username",user.getUsername());
            put("number",user.getNumber());
            put("email",user.getEmail());
            put("city",user.getCity());
            put("province",user.getProvince());
            put("district",user.getDistrict());
            put("gender",user.getGender());
        }};
        userInfo.add(info);
        return new Result(userInfo,200,"成功");
    }

    @PostMapping("/getUserRole/")
    @PreAuthorize("hasAuthority('public_permission')")
    public Result getUserRole(HttpServletRequest request){
        //获取用户ID
        String userId = getTokenInfo.getUserIdByJwt(request);
        String data = userService.getUserRole(userId);
        return new Result(data,200,"成功");
    }
}
